package com.egao.common.module.cms.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egao.common.component.elasticsearch.entity.ElasticSearchConstants;
import com.egao.common.component.elasticsearch.service.ElasticSearchService;
import com.egao.common.core.exception.ParameterException;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.module.cms.entity.Official;
import com.egao.common.module.cms.mapper.OfficialMapper;
import com.egao.common.module.cms.service.OfficialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 信息实现类
 * @author pyx
 */
@Service
public class OfficialServiceImpl extends ServiceImpl<OfficialMapper, Official> implements OfficialService {

    @Autowired
    private ElasticSearchService elasticSearchService;

    @Override
    public List<Official> listPage(PageParam<Official> pageParam) {
        if(pageParam.getOrders().size() == 1 && "create_time".equals(pageParam.getOrders().get(0).getColumn())){
            pageParam.getOrders().get(0).setColumn("co.create_time");
        }

        return baseMapper.listPage(pageParam);
    }

    @Override
    public JsonResult insertOfficial(Official official) {
        if (official == null) {
            throw new ParameterException("参数对象为空");
        }

        if (official.getCrawlerId() == null) {
            throw new ParameterException("类型不能为空");
        }
        if (StrUtil.isBlank(official.getTitle())) {
            throw new ParameterException("标题不能为空");
        }

        baseMapper.insert(official);
        elasticSearchService.save(official);
        return JsonResult.ok("添加成功");
    }

    @Override
    public JsonResult updateOfficial(Official official) {
        if (official == null) {
            throw new ParameterException("参数对象为空");
        }

        if (official.getCrawlerId() == null) {
            throw new ParameterException("类型不能为空");
        }
        if (StrUtil.isBlank(official.getTitle())) {
            throw new ParameterException("标题不能为空");
        }

        baseMapper.updateById(official);
        String id = ElasticSearchConstants.INFORMATION_OFFICIAL + "-" + official.getId();
        elasticSearchService.delete(ElasticSearchConstants.INDEX_NAME_INFORMATION, id);
        elasticSearchService.save(official);
        return JsonResult.ok("修改成功");
    }

    @Override
    public JsonResult deleteOfficial(Integer id) {
        if (id == null) {
            throw new ParameterException("参数对象为空");
        }

        if (removeById(id)) {
            return JsonResult.ok("删除成功");
        }
        String newId = ElasticSearchConstants.INFORMATION_OFFICIAL + "-" + id;
        elasticSearchService.delete(ElasticSearchConstants.INDEX_NAME_INFORMATION, newId);
        return JsonResult.error("删除失败");
    }

    @Override
    public JsonResult getId(Integer id) {
        if (id == null) {
            throw new ParameterException("参数对象为空");
        }

        QueryWrapper<Official> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        Integer count = baseMapper.selectCount(wrapper);

        if (count != 1) {
            throw new ParameterException("信息不存在");
        }

        Official official = baseMapper.selectById(id);
        return JsonResult.ok().setData(official);
    }


}
